import axios from 'axios'
import store from '@/store'

// create an axios instance
const service = axios.create({
  baseURL: process.env.WXSDK_API,
  timeout: 10000, // request timeout
  headers: {
    'Content-Type': 'application/json'
  }
})

// request interceptor
service.interceptors.request.use(config => {
  store.dispatch('setPageLoading', true)

  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

service.interceptors.response.use(response => {
  store.dispatch('setPageLoading', false)
  
  return response
}, error => {
  store.dispatch('setPageLoading', false)

  console.log(error.response) // for debug

  return Promise.reject(error)
})

export default service
